package edu.gatech.cs4400.spring2014.A8.control;

import java.util.HashMap;

import edu.gatech.cs4400.spring2014.A8.view.ApplicationWindow;
import edu.gatech.cs4400.spring2014.A8.view.MainMenuPanel;

/**
 * Controller for the main menu sequence.
 * 
 * @author Stephen
 */
public class MainController implements Controller {
	private static HashMap<MainMenuPanel.Feedback, String> fb2cn; //feedback to classname map
	
	/**
	 * Execute the main menu sequence
	 */
	public void run() {
		//set up the main menu panel and add it to the frame
		MainMenuPanel mainmenu = new MainMenuPanel();
		ApplicationWindow.addView(mainmenu, "mainmenu");
		
		while (true) {
			//reset the screen to show the main menu (initially and after other tasks)
			ApplicationWindow.setView("mainmenu");
			//load the feedback map while the menu is loading
			initFeedback2classname();
			Waiter.waitFor(mainmenu);
			MainMenuPanel.Feedback feedback = mainmenu.getFeedback();
			mainmenu.reset();
			if (feedback == MainMenuPanel.Feedback.LOGOUT) {
				return;
			}
			else {
				//Get and run the controller based on the feedback signal
				String classname = fb2cn.get(feedback);
					try {
						Controller controller = (Controller)Class.forName(classname).newInstance();
						controller.run();	
					} 
					catch (InstantiationException | IllegalAccessException
							| ClassNotFoundException e) {
						System.out.printf("[MainContoller] Exception when instantiaing class %s. Did you rename the file?\n", classname);
						e.printStackTrace();
						System.exit(0);
					}			
			}
		}
	}
	
	/**
	 * Initialize the feedback -> class names map
	 * Used to determine which controller should handle the feedback signal
	 */
	private static void initFeedback2classname() {
		//only the initial call to this method will perform any meaningful operations
		if (fb2cn == null) {
			fb2cn = new HashMap<MainMenuPanel.Feedback, String>();
			String packageName = MainController.class.getPackage().toString();
			packageName = packageName.substring(8);
			fb2cn.put(MainMenuPanel.Feedback.MESSAGES, packageName +".MessagesController");
			fb2cn.put(MainMenuPanel.Feedback.P_EDIT_PROFILE, packageName +".PatientProfileController");
			fb2cn.put(MainMenuPanel.Feedback.P_FIND_APPOINTMENTS, packageName +".PatientFindAppointmentsController");
			fb2cn.put(MainMenuPanel.Feedback.P_EDIT_PAYMENT_INFO, packageName +".EditPaymentController");
			fb2cn.put(MainMenuPanel.Feedback.P_VISIT_HISTORY, packageName +".PatientViewVisitHistoryController");
			fb2cn.put(MainMenuPanel.Feedback.P_RATE, packageName +".RateController");
			fb2cn.put(MainMenuPanel.Feedback.D_EDIT_PROFILE, packageName +".DoctorProfileController");
			fb2cn.put(MainMenuPanel.Feedback.D_VIEW_APPOINTMENTS, packageName +".DoctorViewAppointmentsController");
			fb2cn.put(MainMenuPanel.Feedback.D_PATIENT_HISTORY, packageName +".DoctorViewPatientHistoryController");
			fb2cn.put(MainMenuPanel.Feedback.D_RECORD_VISIT, packageName +".RecordVisitController");
			fb2cn.put(MainMenuPanel.Feedback.D_RECORD_SURGERY, packageName +".RecordSurgeryController");
		}
	}
}
